﻿#region Copyright Info

// 
// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

#region

using System;
using System.Windows;
using System.Windows.Media.Imaging;
using Majesty_of_Omega.Model.Space;

#endregion

namespace Majesty_of_Omega.GUI.Pages.Game.Parts
{
    /// <summary>
    /// OnRaceChanged for the RegionItemPart. This event is raised, to provide info of the region in the owner panel
    /// </summary>
    public class RegionEventArgs : EventArgs
    {
        /// <summary>
        /// Access to the region of the event.
        /// </summary>
        public Region Region
        {
            get { return _region; }
        }

        private readonly Region _region;

        /// <summary>
        /// Constructs an RegionEventArgs for the region
        /// </summary>
        public RegionEventArgs(Region region)
        {
            _region = region;
        }
    }

    /// <summary>
    /// This little user control gives a summary display of a region with a bitmap
    /// of the terrain. This is the code behind for RegionItemPart.xaml
    /// </summary>
    public partial class RegionItemPart
    {
        #region Dependency Properties

        /// <summary>
        /// Using a DependencyProperty as the backing store for Region.  This enables animation, styling, binding, etc... 
        /// </summary>
        public static readonly DependencyProperty RegionProperty =
            DependencyProperty.Register("Region", typeof (Region), typeof (RegionItemPart),
                                        new PropertyMetadata(OnRegionChanged));

        /// <summary>
        /// Gets or sets the Region
        /// </summary>
        public Region Region
        {
            get { return (Region) GetValue(RegionProperty); }
            set { SetValue(RegionProperty, value); }
        }

        #endregion

        #region Delegates

        /// <summary>
        /// Show Region Detail OnRaceChanged signature
        /// </summary>
        /// <param name="sender">RegionItemPart, wich raised the event</param>
        /// <param name="e">OnRaceChanged Info about the region</param>
        public delegate void ShowRegionDetail(object sender, RegionEventArgs e);

        #endregion

        /// <summary>
        /// This OnRaceChanged is raised, when the user wants detailed info about a region
        /// </summary>
        public ShowRegionDetail ShowRegionDetailEvent;


        /// <summary>
        /// Constructor for a Region Item Part.
        /// </summary>
        public RegionItemPart()
        {
            InitializeComponent();
        }


        /// <summary>
        /// Called when [region changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        internal static void OnRegionChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            RegionItemPart p = ((RegionItemPart) sender);
            p.SetRegionData((Region) e.NewValue);
        }

        /// <summary>
        /// Handler for the click event. The user wants to look at the region details
        /// </summary>
        public void ViewRegionDetailButton_Click(Object sender, RoutedEventArgs e)
        {
            if (ShowRegionDetailEvent != null)
            {
                ShowRegionDetailEvent(this, new RegionEventArgs(null));
            }
        }

        /// <summary>
        /// Data binding for the background image didn't work.
        /// </summary>
        /// <param name="region">sets the background image for the region</param>
        /// <remarks>
        /// todo: The uri must be corrected and change the name of this function
        /// </remarks>
        private void SetRegionData(Region region)
        {
            LayoutRoot.DataContext = region;
            BackgroundTerrainImage.Source =
                new BitmapImage(
                    new Uri(@"\Graphics\PlanetStructure\" + Region.RegionTypeInfo.BackgroundImage, UriKind.Relative)
                    );
        }
    }
}